package com.onehouse.infrastructure.repository.room;

import com.onehouse.domain.room.model.RoomTypeEntity;
import com.onehouse.domain.room.repository.IRoomTypeRepository;
import com.onehouse.infrastructure.dao.RoomTypeDao;
import com.onehouse.infrastructure.dao.po.RoomType;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Repository
public class RoomTypeRepository implements IRoomTypeRepository {
    @Resource
    RoomTypeDao roomTypeDao;

    @Override
    public int insert(RoomTypeEntity roomTypeEntity) {
        //RoomTypeEntity 和 RoomType 之间的转换，这样的转换是为了防腐
        RoomType roomType = new RoomType();

        //这里的转换会产生大量的set和get方法，可以考虑使用BeanUtils.copyProperties方法，先跑起来再改
        roomType.setTypeId(roomTypeEntity.getTypeId());
        roomType.setTypeName(roomTypeEntity.getTypeName());
        roomType.setBedType(roomTypeEntity.getBedType());
        roomType.setArea(roomTypeEntity.getArea());
        roomType.setFacilities(roomTypeEntity.getFacilities());
        roomType.setBasePrice(roomTypeEntity.getBasePrice());


        return roomTypeDao.insert(roomType);
    }

    @Override
    public Optional<RoomTypeEntity> selectById(Long typeId) {
        RoomTypeEntity roomTypeEntity = new RoomTypeEntity();
        RoomType roomType = roomTypeDao.selectById(typeId);
        if(roomType == null){
            return Optional.empty();
        } else {
            roomTypeEntity.setTypeId(roomType.getTypeId());
            roomTypeEntity.setTypeName(roomType.getTypeName());
            roomTypeEntity.setBedType(roomType.getBedType());
            roomTypeEntity.setArea(roomType.getArea());
            roomTypeEntity.setFacilities(roomType.getFacilities());
            roomTypeEntity.setBasePrice(roomType.getBasePrice());
            return Optional.of(roomTypeEntity);
        }
    }

    @Override
    public List<RoomTypeEntity> selectList() {
        List<RoomTypeEntity> roomTypeEntities = new ArrayList<>();
        for(RoomType roomType : roomTypeDao.selectList()){
            RoomTypeEntity roomTypeEntity = new RoomTypeEntity();
            roomTypeEntity.setTypeId(roomType.getTypeId());
            roomTypeEntity.setTypeName(roomType.getTypeName());
            roomTypeEntity.setBedType(roomType.getBedType());
            roomTypeEntity.setArea(roomType.getArea());
            roomTypeEntity.setFacilities(roomType.getFacilities());
            roomTypeEntity.setBasePrice(roomType.getBasePrice());
            roomTypeEntities.add(roomTypeEntity);
        }
        return roomTypeEntities;
    }

    @Override
    public int deleteById(Long typeId) {
        return roomTypeDao.deleteById(typeId);
    }

    @Override
    public int updateById(RoomTypeEntity roomTypeEntity) {
        RoomType roomType = new RoomType();

        roomType.setTypeId(roomTypeEntity.getTypeId());
        roomType.setTypeName(roomTypeEntity.getTypeName());
        roomType.setBedType(roomTypeEntity.getBedType());
        roomType.setArea(roomTypeEntity.getArea());
        roomType.setFacilities(roomTypeEntity.getFacilities());
        roomType.setBasePrice(roomTypeEntity.getBasePrice());

        return roomTypeDao.updateById(roomType);
    }

    @Override
    public boolean existedByTypeName(String typeName) {
        return roomTypeDao.existedByTypeName(typeName);
    }
}
